home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 2002 November / SGI Freeware 2002 November - Disc 1.iso / dist / fw_apache2.idb / usr / freeware / apache2 / include / apr_hash.h.z / apr_hash.h
C/C++ Source or Header  |  2002-07-08  |  9KB  |  244 lines

  1. /* ====================================================================
  2.  * The Apache Software License, Version 1.1
  3.  *
  4.  * Copyright (c) 2000-2002 The Apache Software Foundation.  All rights
  5.  * reserved.
  6.  *
  7.  * Redistribution and use in source and binary forms, with or without
  8.  * modification, are permitted provided that the following conditions
  9.  * are met:
  10.  *
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer.
  13.  *
  14.  * 2. Redistributions in binary form must reproduce the above copyright
  15.  *    notice, this list of conditions and the following disclaimer in
  16.  *    the documentation and/or other materials provided with the
  17.  *    distribution.
  18.  *
  19.  * 3. The end-user documentation included with the redistribution,
  20.  *    if any, must include the following acknowledgment:
  21.  *       "This product includes software developed by the
  22.  *        Apache Software Foundation (http://www.apache.org/)."
  23.  *    Alternately, this acknowledgment may appear in the software itself,
  24.  *    if and wherever such third-party acknowledgments normally appear.
  25.  *
  26.  * 4. The names "Apache" and "Apache Software Foundation" must
  27.  *    not be used to endorse or promote products derived from this
  28.  *    software without prior written permission. For written
  29.  *    permission, please contact apache@apache.org.
  30.  *
  31.  * 5. Products derived from this software may not be called "Apache",
  32.  *    nor may "Apache" appear in their name, without prior written
  33.  *    permission of the Apache Software Foundation.
  34.  *
  35.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36.  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38.  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39.  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41.  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42.  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43.  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44.  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45.  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46.  * SUCH DAMAGE.
  47.  * ====================================================================
  48.  *
  49.  * This software consists of voluntary contributions made by many
  50.  * individuals on behalf of the Apache Software Foundation.  For more
  51.  * information on the Apache Software Foundation, please see
  52.  * <http://www.apache.org/>.
  53.  *
  54.  * Portions of this software are based upon public domain software
  55.  * originally written at the National Center for Supercomputing Applications,
  56.  * University of Illinois, Urbana-Champaign.
  57.  */
  58.  
  59. #ifndef APR_HASH_H
  60. #define APR_HASH_H
  61.  
  62. #ifdef __cplusplus
  63. extern "C" {
  64. #endif
  65. /**
  66.  * @file apr_hash.h
  67.  * @brief APR Hash Tables
  68.  */
  69.  
  70. /**
  71.  * @defgroup APR_Hash  Hash Tables
  72.  * @ingroup APR
  73.  * @{
  74.  */
  75.  
  76. #include "apr_pools.h"
  77.  
  78. /**
  79.  * When passing a key to apr_hash_set or apr_hash_get, this value can be
  80.  * passed to indicate a string-valued key, and have apr_hash compute the
  81.  * length automatically.
  82.  *
  83.  * @remark apr_hash will use strlen(key) for the length. The null-terminator
  84.  *         is not included in the hash value (why throw a constant in?).
  85.  *         Since the hash table merely references the provided key (rather
  86.  *         than copying it), apr_hash_this() will return the null-term'd key.
  87.  */
  88. #define APR_HASH_KEY_STRING     (-1)
  89.  
  90. /**
  91.  * Abstract type for hash tables.
  92.  */
  93. typedef struct apr_hash_t apr_hash_t;
  94.  
  95. /**
  96.  * Abstract type for scanning hash tables.
  97.  */
  98. typedef struct apr_hash_index_t apr_hash_index_t;
  99.  
  100. /**
  101.  * Create a hash table.
  102.  * @param pool The pool to allocate the hash table out of
  103.  * @return The hash table just created
  104.   */
  105. APR_DECLARE(apr_hash_t *) apr_hash_make(apr_pool_t *pool);
  106.  
  107. /**
  108.  * Make a copy of a hash table
  109.  * @param pool The pool from which to allocate the new hash table
  110.  * @param h The hash table to clone
  111.  * @return The hash table just created
  112.  * @remark Makes a shallow copy
  113.  */
  114. APR_DECLARE(apr_hash_t *) apr_hash_copy(apr_pool_t *pool,
  115.                                         const apr_hash_t *h);
  116.  
  117. /**
  118.  * Associate a value with a key in a hash table.
  119.  * @param ht The hash table
  120.  * @param key Pointer to the key
  121.  * @param klen Length of the key. Can be APR_HASH_KEY_STRING to use the string length.
  122.  * @param val Value to associate with the key
  123.  * @remark If the value is NULL the hash entry is deleted.
  124.  */
  125. APR_DECLARE(void) apr_hash_set(apr_hash_t *ht, const void *key,
  126.                                apr_ssize_t klen, const void *val);
  127.  
  128. /**
  129.  * Look up the value associated with a key in a hash table.
  130.  * @param ht The hash table
  131.  * @param key Pointer to the key
  132.  * @param klen Length of the key. Can be APR_HASH_KEY_STRING to use the string length.
  133.  * @return Returns NULL if the key is not present.
  134.  */
  135. APR_DECLARE(void *) apr_hash_get(apr_hash_t *ht, const void *key,
  136.                                  apr_ssize_t klen);
  137.  
  138. /**
  139.  * Start iterating over the entries in a hash table.
  140.  * @param p The pool to allocate the apr_hash_index_t iterator. If this
  141.  *          pool is NULL, then an internal, non-thread-safe iterator is used.
  142.  * @param ht The hash table
  143.  * @example
  144.  */
  145. /**
  146.  * <PRE>
  147.  * 
  148.  *     int sum_values(apr_pool_t *p, apr_hash_t *ht)
  149.  *     {
  150.  *         apr_hash_index_t *hi;
  151.  *        void *val;
  152.  *        int sum = 0;
  153.  *        for (hi = apr_hash_first(p, ht); hi; hi = apr_hash_next(hi)) {
  154.  *            apr_hash_this(hi, NULL, NULL, &val);
  155.  *            sum += *(int *)val;
  156.  *        }
  157.  *        return sum;
  158.  *     }
  159.  * 
  160.  * There is no restriction on adding or deleting hash entries during an
  161.  * iteration (although the results may be unpredictable unless all you do
  162.  * is delete the current entry) and multiple iterations can be in
  163.  * progress at the same time.
  164.  * </PRE>
  165.   */
  166. APR_DECLARE(apr_hash_index_t *) apr_hash_first(apr_pool_t *p, apr_hash_t *ht);
  167.  
  168. /**
  169.  * Continue iterating over the entries in a hash table.
  170.  * @param hi The iteration state
  171.  * @return a pointer to the updated iteration state.  NULL if there are no more  
  172.  *         entries.
  173.  */
  174. APR_DECLARE(apr_hash_index_t *) apr_hash_next(apr_hash_index_t *hi);
  175.  
  176. /**
  177.  * Get the current entry's details from the iteration state.
  178.  * @param hi The iteration state
  179.  * @param key Return pointer for the pointer to the key.
  180.  * @param klen Return pointer for the key length.
  181.  * @param val Return pointer for the associated value.
  182.  * @remark The return pointers should point to a variable that will be set to the
  183.  *         corresponding data, or they may be NULL if the data isn't interesting.
  184.  */
  185. APR_DECLARE(void) apr_hash_this(apr_hash_index_t *hi, const void **key, 
  186.                                 apr_ssize_t *klen, void **val);
  187.  
  188. /**
  189.  * Get the number of key/value pairs in the hash table.
  190.  * @param ht The hash table
  191.  * @return The number of key/value pairs in the hash table.
  192.  */
  193. APR_DECLARE(int) apr_hash_count(apr_hash_t *ht);
  194.  
  195. /**
  196.  * Merge two hash tables into one new hash table. The values of the overlay
  197.  * hash override the values of the base if both have the same key.
  198.  * @param p The pool to use for the new hash table
  199.  * @param overlay The table to add to the initial table
  200.  * @param base The table that represents the initial values of the new table
  201.  * @return A new hash table containing all of the data from the two passed in
  202.  */
  203. APR_DECLARE(apr_hash_t *) apr_hash_overlay(apr_pool_t *p,
  204.                                            const apr_hash_t *overlay, 
  205.                                            const apr_hash_t *base);
  206.  
  207. /**
  208.  * Merge two hash tables into one new hash table. If the same key
  209.  * is present in both tables, call the supplied merge function to
  210.  * produce a merged value for the key in the new table.
  211.  * @param p The pool to use for the new hash table
  212.  * @param h1 The first of the tables to merge
  213.  * @param h2 The second of the tables to merge
  214.  * @param merger A callback function to merge values, or NULL to
  215.  *  make values from h1 override values from h2 (same semantics as
  216.  *  apr_hash_overlay())
  217.  * @param data Client data to pass to the merger function
  218.  * @return A new hash table containing all of the data from the two passed in
  219.  */
  220. APR_DECLARE(apr_hash_t *) apr_hash_merge(apr_pool_t *p,
  221.                                          const apr_hash_t *h1,
  222.                                          const apr_hash_t *h2,
  223.                                          void * (*merger)(apr_pool_t *p,
  224.                                                      const void *key,
  225.                                                      apr_ssize_t klen,
  226.                                                      const void *h1_val,
  227.                                                      const void *h2_val,
  228.                                                      const void *data),
  229.                                          const void *data);
  230.  
  231. /**
  232.  * Get a pointer to the pool which the hash table 
  233.  * was created in
  234.  * @param hash the hash table in question
  235.  */
  236. APR_POOL_DECLARE_ACCESSOR(hash);
  237.  
  238. /** @} */
  239. #ifdef __cplusplus
  240. }
  241. #endif
  242.  
  243. #endif    /* !APR_HASH_H */
  244.